<template>
  <view class="container">
    <view class="product-header">
      <image :src="product.image" mode="aspectFill" class="product-image"></image>
    </view>
    
    <view class="product-info">
      <view class="product-category">{{ product.category }}</view>
      <view class="product-name">{{ product.name }}</view>
      <view class="product-price">¥{{ product.price }}</view>
      <view class="product-description">{{ product.description }}</view>
    </view>
    
    <view class="product-details">
      <view class="detail-item">
        <text class="detail-label">月销量</text>
        <text class="detail-value">{{ product.sales || 0 }}件</text>
      </view>
      <view class="detail-item">
        <text class="detail-label">库存</text>
        <text class="detail-value">充足</text>
      </view>
    </view>
    
    <view class="action-bar">
      <view class="quantity-control">
        <text class="quantity-btn" @click="decreaseQuantity">-</text>
        <text class="quantity-value">{{ quantity }}</text>
        <text class="quantity-btn" @click="increaseQuantity">+</text>
      </view>
      <view class="action-buttons">
        <button class="add-cart-btn" @click="addToCart">加入购物车</button>
        <button class="buy-btn" @click="buyNow">立即购买</button>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      product: {
        id: '',
        name: '',
        price: 0,
        image: '',
        description: '',
        sales: 0,
        category: ''
      },
      quantity: 1
    };
  },
  onLoad(options) {
    // 从路由参数中获取商品信息
    this.product = {
      id: options.id,
      name: decodeURIComponent(options.name),
      price: parseFloat(options.price),
      image: decodeURIComponent(options.image),
      description: decodeURIComponent(options.description),
      sales: parseInt(options.sales) || 0,
      category: decodeURIComponent(options.category)
    };
  },
  methods: {
    decreaseQuantity() {
      if (this.quantity > 1) {
        this.quantity--;
      }
    },
    increaseQuantity() {
      this.quantity++;
    },
    addToCart() {
      // 构建购物车商品信息
      const cartItem = {
        ...this.product,
        quantity: this.quantity
      };
      
      // 获取现有的购物车数据
      let cart = uni.getStorageSync('cart') || [];
      
      // 检查是否已存在相同商品
      const existingItemIndex = cart.findIndex(item => item.id === cartItem.id);
      if (existingItemIndex !== -1) {
        // 如果存在，更新数量
        cart[existingItemIndex].quantity += cartItem.quantity;
      } else {
        // 如果不存在，添加新商品
        cart.push(cartItem);
      }
      
      // 保存到本地存储
      uni.setStorageSync('cart', cart);
      
      uni.showToast({
        title: '已加入购物车',
        icon: 'success'
      });
    },
    buyNow() {
      // 构建订单信息
      const orderInfo = {
        product: this.product,
        quantity: this.quantity,
        totalPrice: this.product.price * this.quantity
      };
      
      // 跳转到订单确认页
      uni.navigateTo({
        url: `/pages/order/confirm?orderInfo=${encodeURIComponent(JSON.stringify(orderInfo))}`
      });
    }
  }
};
</script>

<style>
.container {
  min-height: 100vh;
  background-color: #f8f8f8;
  padding-bottom: 120rpx;
}

.product-header {
  width: 100%;
  height: 600rpx;
  background-color: #fff;
}

.product-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.product-info {
  background-color: #fff;
  padding: 30rpx;
  margin-top: 20rpx;
}

.product-name {
  font-size: 36rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 20rpx;
}

.product-price {
  font-size: 40rpx;
  color: #ff4d4f;
  font-weight: bold;
  margin-bottom: 20rpx;
}

.product-description {
  font-size: 28rpx;
  color: #666;
  line-height: 1.6;
}

.product-details {
  background-color: #fff;
  margin-top: 20rpx;
  padding: 30rpx;
}

.detail-item {
  display: flex;
  justify-content: space-between;
  padding: 20rpx 0;
  border-bottom: 1rpx solid #f5f5f5;
}

.detail-item:last-child {
  border-bottom: none;
}

.detail-label {
  font-size: 28rpx;
  color: #666;
}

.detail-value {
  font-size: 28rpx;
  color: #333;
}

.action-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background-color: #fff;
  padding: 20rpx;
  display: flex;
  align-items: center;
  box-shadow: 0 -2rpx 10rpx rgba(0,0,0,0.05);
}

.quantity-control {
  display: flex;
  align-items: center;
  margin-right: 30rpx;
}

.quantity-btn {
  width: 60rpx;
  height: 60rpx;
  background-color: #f5f5f5;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 32rpx;
  color: #333;
}

.quantity-value {
  width: 80rpx;
  text-align: center;
  font-size: 32rpx;
  color: #333;
}

.action-buttons {
  flex: 1;
  display: flex;
}

.add-cart-btn, .buy-btn {
  flex: 1;
  height: 80rpx;
  line-height: 80rpx;
  text-align: center;
  border-radius: 40rpx;
  font-size: 28rpx;
  margin: 0 10rpx;
}

.add-cart-btn {
  background-color: #ffd700;
  color: #333;
}

.buy-btn {
  background-color: #4CAF50;
  color: #fff;
}

.product-category {
  font-size: 24rpx;
  color: #999;
  margin-bottom: 10rpx;
}
</style> 